package com.good.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.good.entity.Goo;

@Controller
public class IndexController {
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(HttpSession session,Model model) {
		/*ServletContext application = session.getServletContext();
		HashSet sessions=(HashSet)application.getAttribute("sessions");
		model.addAttribute("sessionsize", sessions.size());*/
		return "/index";
	}
	@RequestMapping(value="redirectDemo",method=RequestMethod.GET)
	public String redirectDemo(RedirectAttributes redirectAttributes ){
		redirectAttributes.addAttribute("redirect", "redirect"); 
		return "redirect:/index.do";
	}
	@RequestMapping(value="/ajax")
	@ResponseBody
	public Map<String,Object> ajax(){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("result", 1);
		return map;
	}
	@RequestMapping(value="/chbox")
	public String chebox(@RequestParam(value="chbox") String chbox){
		System.out.println("");
		System.out.println(chbox);
		System.out.println(chbox.split(",")[0]);
		return "success";
	}
	@RequestMapping(value="/good1",method=RequestMethod.DELETE)
	public void good(@RequestParam Long id){
		System.out.println(id);
		System.out.println("good");
	}
	@RequestMapping(value="/getDemo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getDemo(){
		Map<String,Object> map=new HashMap<String, Object>();
		System.out.print("getDemo");
		map.put("get", "success");
		return map;
	}
	@RequestMapping(value="/postDemo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> postDemo(@RequestBody Goo goo){
		System.out.println(goo.getGood());
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("post", "success");
		return map;
	}
	@RequestMapping(value="/postDemo2",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> postDemo2(@RequestBody String good){
		System.out.println(good);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("post2", "success");
		return map;
	}
	@RequestMapping(value="/putDemo",method=RequestMethod.PUT)
	@ResponseBody
	public Map<String,Object> putDemo(@RequestBody String better){
		System.out.println(better);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("put", "success");
		return map;
	}
	@RequestMapping(value="/deleteDemo",method=RequestMethod.DELETE)
	@ResponseBody
	public Map<String,Object> deleteDemo(){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("delete", "success");
		return map;
	}
}
